package com.zb.controller;

import com.zb.pojo.Book;
import com.zb.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zhs
 * @create 2020-07-22-11:40
 */
@Controller
@RequestMapping(value ="/book" )
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping(value ="/list" )
    @ResponseBody
    public List<Book> list(){
//        List<Book> bookList=new ArrayList<Book>();
//        bookList.add(new Book(1,"java","张三",100D,new Date()));
//        bookList.add(new Book(2,"html","李四",80D,new Date()));
//        return bookList;
        return bookService.findAll();
    }


    @RequestMapping(value = "/findById/{id}")
    @ResponseBody
    public  Book findById(@PathVariable("id") Integer id){
       return bookService.findById(id);
    }

    /**
     * 添加
     * @param book
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public String add(Book book){
    //  Integer count=bookService.add(book);
    //  if(count>0){
          return "添加成功";
    //  }else {
        //     return "添加失败";
      //}
    }

    /**
     * 修改
     * @param book
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public String update(Book book){
        //Integer count= bookService.update(book);
        //if(count>0){
            return "修改成功";
       // }else {
       //     return "修改失败";
       // }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById/{id}")
    @ResponseBody
    public String deleteById(@PathVariable("id") Integer id){
        Integer count= bookService.deleteById(id);
        if(count>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

}
